package com.gsi.webstore.platform.storefront.pipelet;

import com.gsi.webstore.platform.adapter.storelocator.internal.client.data.Storefront;
import com.intershop.beehive.bts.capi.orderprocess.LineItem;
import com.intershop.beehive.core.capi.pipeline.Pipelet;
import com.intershop.beehive.core.capi.pipeline.PipeletExecutionException;
import com.intershop.beehive.core.capi.pipeline.PipelineDictionary;
import com.intershop.component.shipping_data.capi.shippingmethod.ShippingMethod;

/**
 * Adds store front info to a cart line item.
 */
public class AddStoreFrontToLineItem extends Pipelet
{

    /**
     * Constant used to access the pipeline dictionary with key 'LineItem'
     * 
     * The cart line item object
     */
    public static final String DN_LINE_ITEM = "LineItem";
    
    /**
     * Constant used to access the pipeline dictionary with key 'Storefront'
     * 
     * The store front object
     */
    public static final String DN_STOREFRONT = "Storefront";

    /**
     * Constant used to access the pipeline dictionary with key 'ShippingMethod'
     * 
     * The shipping method object
     */
    public static final String DN_SHIPPING_METHOD = "ShippingMethod";

    /**
     * The pipelet's execute method is called whenever the pipelets gets
     * executed in the context of a pipeline and a request. The pipeline
     * dictionary valid for the currently executing thread is provided as a
     * parameter.
     * 
     * @param dict
     *            The pipeline dictionary to be used.
     * @throws PipeletExecutionException
     *             Thrown in case of severe errors that make the pipelet execute
     *             impossible (e.g. missing required input data).
     */
    public int execute(PipelineDictionary dict)
    throws PipeletExecutionException {        
       
        // lookup 'ShippingMethod' in pipeline dictionary
        ShippingMethod shippingMethod = (ShippingMethod)dict.get(DN_SHIPPING_METHOD);
        
               
        // lookup 'Storefront' in pipeline dictionary
        Storefront storefront = (Storefront)dict.get(DN_STOREFRONT);
        
        
        // lookup 'LineItem' in pipeline dictionary
        LineItem lineItem = (LineItem)dict.get(DN_LINE_ITEM);
        if (null == lineItem)
        {
            throw new PipeletExecutionException("Mandatory input parameter 'LineItem' not available in pipeline dictionary.");
        }

        if (storefront != null && shippingMethod != null) {
            
            lineItem.putLong("STORE_ID", storefront.getId());
            lineItem.putString("STORE_CODE", storefront.getStoreCode());            

            // set shipping method to ISPU or STS
            lineItem.setShippingMethod(shippingMethod);
            
            
            
            // TODO: copy store shipping address into the shipping address for the item.  If store only
            // TODO: has a store front address then copy it in.
            // Address shipToAddress = new ????
            // lineItem.setShipToAddress(shipToAddress);

        } else {
            return PIPELET_ERROR;
        }
        
        return PIPELET_NEXT;
    }
}